package com.svo.weitu.adapter;

import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.svo.platform.utils.PicUtil;
import com.svo.weitu.App;
import com.svo.weitu.R;
import com.svo.weitu.util.Utils;

import org.json.JSONObject;

import java.util.List;

public class SearchAdapter extends BaseQuickAdapter<JSONObject,BaseViewHolder> {
	List<JSONObject> data;
	public SearchAdapter(List<JSONObject> data) {
		super(R.layout.search_list_item,data);
		this.data = data;
	}


	@Override
	protected void convert(BaseViewHolder helper, JSONObject entity) {
//		helper.setText(R.id.title,Html.fromHtml(entity.optString("title","无")));
//				.setText(R.id.info1Tv,item.getTime())
//				.setText(R.id.info2Tv,item.getTag());
//		helper.getView(R.id.root).setOnClickListener(new ClickListener(item));
		ImageView imageView = helper.getView(R.id.img);
		ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
		layoutParams.height = getHeight(entity);
		layoutParams.width = Utils.getScreenWidth()/2;
		imageView.setLayoutParams(layoutParams);
		String thumbnail = entity.optString("thumburl","");
		if (TextUtils.isEmpty(thumbnail)) {
			thumbnail = entity.optString("obj_url","");
			if (TextUtils.isEmpty(thumbnail)) {
				PicUtil.displayImage(App.context, thumbnail, imageView);
			}
		}else {
			PicUtil.displayImage(App.context, thumbnail, imageView);
		}
	}

	private int getHeight(JSONObject entity) {
		int width = entity.optInt("width");
		int height = entity.optInt("height");
		int screenWidth = Utils.getScreenWidth()/2;
		float xishu = screenWidth / (float)width;
		return (int) (height*xishu);
	}

}
